package meterMonitor.util;

import meterMonitor.dto.mqttApi.KeyAndNameDto;
import meterMonitor.dto.mqttApi.KeyAndNameResponse;
import meterMonitor.dto.mqttApi.ResponseDto;
import meterMonitor.model.Meter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by Administrator on 2019/2/24 0024.
 */
@Service("mqttApiUtil")
public class MqttApiUtil {
    @Autowired
    private ResourceConfig resourceConfig;

    private String getApiUrl() {
        return resourceConfig.getValueByKey("mqtt_api_url");
    }
    private String getUserName() {
        return resourceConfig.getValueByKey("mqtt_api_username");
    }
    private String getPassword() {
        return resourceConfig.getValueByKey("mqtt_api_password");
    }

    public String getToken(){
        String url=this.getApiUrl()+"/oauth/oauth/token";
        String requestBody="username="+this.getUserName()+"&password="+this.getPassword()+"&grant_type=password";
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization", "Basic Y2xpZW50MToxMjM0NTY=");
        headers.set("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity<String> httpEntity=RestfulUtils.buildRequestEntity(requestBody,headers);
        ResponseEntity<String> responseEntity=RestfulUtils.exchange(url,httpEntity,HttpMethod.POST);
        String body=responseEntity.getBody();
        if(StringUtils.isBlank(body)){
            throw new BizException("请求响应为空");
        }
        Map<String,Object> mapData=JacksonMapper.jsonToMap(body);
        String token=null;
        if(mapData.containsKey("access_token")){
            token=(String) mapData.get("access_token");
        }
        return token;
    }

    public KeyAndNameResponse getWakeConfig(Meter meter, String token){
        String url=this.getApiUrl()+"/manage/wakeConfig/keyAndName";
        // String param="?deviceName="+meter.getDeviceName()+"&productKey="+meter.getProductKey()+"&access_token="+token;
        String param="?deviceName="+meter.getDeviceName()+"&access_token="+token;
        url+=param;
        String responseStr= RestfulUtils.doGetRestfullRequest(url);
        KeyAndNameResponse responseDto=JacksonMapper.toObject(responseStr,KeyAndNameResponse.class);
        return responseDto;
    }

    public boolean setIntervalTime(String token,KeyAndNameDto keyAndNameDto){
        String url=this.getApiUrl()+"/manage/wakeConfig";
        String param="?productKey="+keyAndNameDto.getProductKey()+"&deviceName="+keyAndNameDto.getDeviceName()+"&access_token="+token;
        if(keyAndNameDto.getMode()!=null){
            //param+="&mode="+keyAndNameDto.getMode();
            param+="&mode="+1;
        }else{
            param+="&mode=1";
        }
        if(keyAndNameDto.getHour()!=null){
            param+="&hour="+keyAndNameDto.getHour();
        }else{
            param+="&hour=7";
        }
        if(keyAndNameDto.getMinute()!=null){
            param+="&minute="+keyAndNameDto.getMinute();
        }else{
            param+="&minute=0";
        }
        if(keyAndNameDto.getInterval()!=null&&keyAndNameDto.getInterval().compareTo(0L)>0){
            param+="&interval="+keyAndNameDto.getInterval();
        }else{
            param+="&interval=20";
        }
        if(keyAndNameDto.getId()!=null){
            param+="&id="+keyAndNameDto.getId();
        }else{
            param+="&id=0";
        }
        url+=param;
        HttpEntity<String> httpEntity=RestfulUtils.buildRequestEntity("",null);
        ResponseEntity<String> responseEntity=RestfulUtils.exchange(url,httpEntity,HttpMethod.PUT);
        String body=responseEntity.getBody();
        if(StringUtils.isBlank(body)){
            throw new BizException("请求响应为空");
        }
        Map<String,Object> mapData=JacksonMapper.jsonToMap(body);
        if(mapData.containsKey("code")){
            Integer code=(Integer) mapData.get("code");
            if(code.intValue()==200){
                return true;
            }
        }
        return false;
    }
}
